package com.useeinfo.oa.open.api;

import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.HttpClient;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * app日历模块
 */
@Controller
@RequestMapping("/api/calendarOrderAPI")
public class CalendarOrderAPI {
    @Autowired
    private HttpServletRequest request;

    @RequestMapping("/calenderByOrderList")
    @ResponseBody
    public JSONObject calenderByOrderList(String date,String type,String pageSize,String pageIndex){
        //type repair:维修，maintain:维保，inspection :巡检 ，task ：任务工单
        JSONObject jsonObject=new JSONObject();
        Map<String, Object> dataMap = new HashMap<>();
        String url = "";
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        SystemUser user = SessionKeyUtils.getSessionUser(request);

        if(null == user || null == enterprise){
           jsonObject.put("status","1");
           jsonObject.put("msg","失败");
           jsonObject.put("data","");
           return jsonObject;
        }
        dataMap.put("pageIndex",pageIndex);
        dataMap.put("pageSize",pageSize);
        dataMap.put("userId", StringConverters.null2String(user.getId()));
        dataMap.put("enterpriseId",StringConverters.null2String(enterprise.getId()));
        dataMap.put("date",date);
        if("repair".equals(type)){
            dataMap.put("status","2");
            url = Configuration.getConfigurationByName("hostUrl") + "/repairCalendarAPI/list.action";
            jsonObject =  JSONObject.fromObject(HttpClient.doPost(url, dataMap));
        }
        if("maintain".equals(type)){
            dataMap.put("status","1");
            url = Configuration.getConfigurationByName("hostUrl") + "/maintainCalendarAPI/list.action";
            jsonObject =  JSONObject.fromObject(HttpClient.doPost(url, dataMap));
        }
        if("inspection".equals(type)){
            dataMap.put("status","1");
            String ppmDataUrl = Configuration.getConfigurationByName("hostUrl") + "/inspectionCalendarAPI/list.action";
            jsonObject=  JSONObject.fromObject(HttpClient.doPost(ppmDataUrl, dataMap));
        }
        if("task".equals(type)){
//            dataMap.put("status","1");
            dataMap.put("status","2");
             url = Configuration.getConfigurationByName("hostUrl") + "/taskCalendarAPI/list.action";
            jsonObject=  JSONObject.fromObject(HttpClient.doPost(url, dataMap));
        }
        return jsonObject;
    }

    @RequestMapping("/calenderMonth")
    @ResponseBody
    public JSONObject calenderMonth(String monthParam){//月份格式 yyyy-MM
        JSONObject result = new JSONObject();
        JSONArray rows = new JSONArray();
        JSONObject data = new JSONObject();
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        SystemUser user = SessionKeyUtils.getSessionUser(request);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("pageIndex",1);
        dataMap.put("pageSize",1);
        dataMap.put("userId", StringConverters.null2String(user.getId()));
        dataMap.put("enterpriseId",StringConverters.null2String(enterprise.getId()));
        dataMap.put("status","1");
        Date monthBeginDate = DateTimeUtils.parse("yyyy-MM",monthParam);
        int year = monthBeginDate.getYear();
        int month = monthBeginDate.getMonth();
        Calendar c = Calendar.getInstance();
        c.set(year, month, 1);
        int total = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < total; i++) {
            JSONObject dayJson = new JSONObject();
            String day = DateTimeUtils.formatDateToString(DateTimeUtils.addDayOfMonth(monthBeginDate,i));
            dayJson.put("day",day);
            dataMap.put("date",day);
            //查询当天是否有维修记录
            String repairUrl = Configuration.getConfigurationByName("hostUrl") + "/repairCalendarAPI/list.action";
            JSONObject repairResult =  JSONObject.fromObject(HttpClient.doPost(repairUrl, dataMap));
            try {
                int records = repairResult.getJSONObject("data").getInt("records");
                if(records>0){
                    dayJson.put("repair",true);
                }else{
                    dayJson.put("repair",false);
                }
            }catch (Exception e){
                dayJson.put("repair",false);
            }
            //查询当天是否有维保记录
            String maintainUrl = Configuration.getConfigurationByName("hostUrl") + "/maintainCalendarAPI/list.action";
            JSONObject maintainResult =  JSONObject.fromObject(HttpClient.doPost(maintainUrl, dataMap));
            try {
                int records = maintainResult.getJSONObject("data").getInt("records");
                if(records>0){
                    dayJson.put("maintain",true);
                }else{
                    dayJson.put("maintain",false);
                }
            }catch (Exception e){
                dayJson.put("maintain",false);
            }
            //查询当天是否有巡检记录
            String inspectionUrl = Configuration.getConfigurationByName("hostUrl") + "/inspectionCalendarAPI/list.action";
            JSONObject inspectionResult =  JSONObject.fromObject(HttpClient.doPost(inspectionUrl, dataMap));
            try {
                int records = inspectionResult.getJSONObject("data").getInt("records");
                if(records>0){
                    dayJson.put("inspection",true);
                }else{
                    dayJson.put("inspection",false);
                }
            }catch (Exception e){
                dayJson.put("inspection",false);
            }

            //查询当天是否有任务单记录
            String taskUrl = Configuration.getConfigurationByName("hostUrl") + "/taskCalendarAPI/list.action";
            JSONObject taskResult =  JSONObject.fromObject(HttpClient.doPost(taskUrl, dataMap));
            try {
                int records = taskResult.getJSONObject("data").getInt("records");
                if(records>0){
                    dayJson.put("task",true);
                }else{
                    dayJson.put("task",false);
                }
            }catch (Exception e){
                dayJson.put("task",false);
            }
            rows.add(dayJson);
        }
        result.put("status",0);
        result.put("msg","OK");
        data.put("rows",rows);
        result.put("data",data);
        return result;
    }
}
